package org.blame.rabbitmqadvancedfeature.listener;


import com.rabbitmq.client.Channel;
import org.blame.rabbitmqadvancedfeature.constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

@Component
public class AckListener {

    @RabbitListener(queues = Constants.ACK_QUEUE)
    public void ListenerQueue(Message message, Channel channel) throws IOException {

        System.out.printf("接收到的消息:%s, deliveryTag: %d \n",new String(message.getBody(), StandardCharsets.UTF_8)
                ,message.getMessageProperties().getDeliveryTag());

        long getDeliveryTag=message.getMessageProperties().getDeliveryTag();
        try {
            int sum=3/0;
//            multiple如果为true，则是批量确认，false则不是
            channel.basicAck(getDeliveryTag,false);
        }catch (Exception e){

            //requeue如果为true，消息发送失败会回到队列，重新发送，如果false，则会直接抛弃w
            channel.basicNack(getDeliveryTag,false,false);
        }
    }

//    @RabbitListener(queues = Constants.ACK_QUEUE)
//    public void ListenerQueue2(Message message, Channel channel) throws IOException {
//
//        System.out.printf("接收到的消息:%s, deliveryTag: %d \n",new String(message.getBody(), StandardCharsets.UTF_8)
//                ,message.getMessageProperties().getDeliveryTag());
//
//
//    }
}
